package com.bx.imserver.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableCaching
public class RedisConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean(name = "redisTemplateDB0")
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplateDB0() {
        return createRedisTemplate(0);
    }

    @Bean(name = "redisTemplateDB2")
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplateDB2() {
        return createRedisTemplate(2);
    }

    // 其他数据库的RedisTemplate...

    private RedisTemplate<Object, Object> createRedisTemplate(int dbIndex) {
        RedisConnectionFactory connectionFactory = cloneConnectionFactory(redisConnectionFactory, dbIndex);
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }

    private RedisConnectionFactory cloneConnectionFactory(RedisConnectionFactory factory, int dbIndex) {
        // 假设factory是LettuceConnectionFactory的实例
        if (factory instanceof LettuceConnectionFactory) {
            LettuceConnectionFactory lettuceFactory = (LettuceConnectionFactory) factory;

            // 创建新的LettuceConnectionFactory的实例，并复制现有的配置
            LettuceConnectionFactory newFactory = new LettuceConnectionFactory();
            newFactory.setHostName(lettuceFactory.getHostName());
            newFactory.setPort(lettuceFactory.getPort());
            newFactory.setPassword(lettuceFactory.getPassword());
            newFactory.setDatabase(dbIndex); // 设置你想要的数据库索引
            newFactory.setClientName(lettuceFactory.getClientName());
            newFactory.setUseSsl(lettuceFactory.isUseSsl());
            newFactory.setTimeout(lettuceFactory.getTimeout());

            // 如果你还需要设置其他的连接配置，此处继续添加

            newFactory.afterPropertiesSet(); // 初始化工厂

            return newFactory;
        } else {
            // 如果factory不是LettuceConnectionFactory的实例，抛出异常或者进行其他处理
            throw new IllegalArgumentException("The provided factory is not an instance of LettuceConnectionFactory");
        }
    }


}
